//package com.wulian.chaincode;
//
//import com.owlike.genson.Genson;
//import org.hyperledger.fabric.contract.Context;
//import org.hyperledger.fabric.contract.ContractInterface;
//import org.hyperledger.fabric.contract.annotation.*;
//import org.hyperledger.fabric.shim.ChaincodeException;
//import org.hyperledger.fabric.shim.ChaincodeStub;
//import org.hyperledger.fabric.shim.ledger.KeyValue;
//import org.hyperledger.fabric.shim.ledger.QueryResultsIterator;
//
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * @author xf
// * @date 2021/3/22 下午 11:32
// * @Description
// */
//@Contract(
//        name = "record",
//        info = @Info(
//                title = "Record",
//                description = "The hyperlegendary asset transfer",
//                version = "0.0.1-SNAPSHOT",
//                license = @License(
//                        name = "Apache 2.0 License",
//                        url = "http://www.apache.org/licenses/LICENSE-2.0.html"),
//                contact = @Contact(
//                        email = "a.transfer@example.com",
//                        name = "Adrian Transfer",
//                        url = "https://hyperledger.example.com")
//        )
//)
//@Default
//public final class RecordTransfer implements ContractInterface {
//
//    private final Genson genson = new Genson();
//
//    private enum RecordTransferErrors{
//        RECORD_NOT_FOUND,
//        RECORD_ALREADY_EXISTS
//    }
//
//
//    /**
//     * 初始化
//     * @param context
//     */
//    @Transaction(intent = Transaction.TYPE.SUBMIT)
//    public void InitLedger(final Context context){
//        ChaincodeStub stub = context.getStub();
//
//        CreateRecord(context,"123456","张二","感冒","无","按时吃药","李好","呼吸道科","娄底中心医院","20210310");
//        CreateRecord(context,"123457","张三","感冒","无","按时吃药","李好","呼吸道科","娄底中心医院","20210310");
//        CreateRecord(context,"123458","张五","感冒","无","按时吃药","李好","呼吸道科","娄底中心医院","20210310");
//        CreateRecord(context,"123459","张六","感冒","无","按时吃药","李好","呼吸道科","娄底中心医院","20210310");
//    }
//
//
//    /**
//     * 上传病历
//     * @param context
//     * @param recordId
//     * @param patientName
//     * @param symptom
//     * @param check
//     * @param treatPlan
//     * @param doctorName
//     * @param department
//     * @param hospitalName
//     * @param createTime
//     * @return
//     */
//    @Transaction(intent = Transaction.TYPE.SUBMIT)
//    public Record CreateRecord(final Context context,
//                               final String recordId,
//                               final String patientName,
//                               final String symptom,
//                               final String check,
//                               final String treatPlan,
//                               final String doctorName,
//                               final String department,
//                               final String hospitalName,
//                               final String createTime
//                               ){
//        ChaincodeStub stub = context.getStub();
//        if (RecordExists(context,recordId)){
//            String errorMessage = String.format("病历 %s 已经存在", recordId);
//            System.out.println(errorMessage);
//            throw new ChaincodeException(errorMessage, RecordTransferErrors.RECORD_ALREADY_EXISTS.toString());
//        }
//
//        Record record = new Record(recordId, patientName, symptom, check, treatPlan, doctorName, department, hospitalName, createTime);
//        String recordJSON = genson.serialize(record);
//        stub.putStringState(recordId,recordJSON);
//        return record;
//    }
//
//
//    /**
//     * 检查病历是否存在
//     * @param context
//     * @param recordId
//     * @return
//     */
//    @Transaction(intent = Transaction.TYPE.EVALUATE)
//    public boolean RecordExists(final Context context,final String recordId){
//        ChaincodeStub stub = context.getStub();
//        String recordJSON = stub.getStringState(recordId);
//        return (recordJSON!=null && !recordJSON.isEmpty());
//    }
//
//
//    /**
//     * 查询病历
//     * @param context
//     * @param recordId
//     * @return
//     */
//    @Transaction(intent = Transaction.TYPE.EVALUATE)
//    public Record GetRecordById(final Context context,final String recordId){
//        ChaincodeStub stub = context.getStub();
//        String recordJSON = stub.getStringState(recordId);
//
//        if (recordJSON == null || recordJSON.isEmpty()){
//            String errorMessage = String.format("病历 %s 不能查询到", recordId);
//            System.out.println(errorMessage);
//            throw new ChaincodeException(errorMessage, RecordTransferErrors.RECORD_NOT_FOUND.toString());
//        }
//
//        Record record = genson.deserialize(recordJSON, Record.class);
//
//        return record;
//    }
//
//
//    /**
//     *
//     * @param context
//     * @return
//     */
//    @Transaction(intent = Transaction.TYPE.EVALUATE)
//    public String GetAllRecord(final Context context){
//        ChaincodeStub stub = context.getStub();
//        List<Record> queryResults = new ArrayList<>();
//
//        QueryResultsIterator<KeyValue> results = stub.getStateByRange("", "");
//
//        for (KeyValue result:results){
//            Record record = genson.deserialize(result.getStringValue(), Record.class);
//            queryResults.add(record);
//            System.out.println(record.toString());
//        }
//
//        final String response = genson.serialize(queryResults);
//
//        return response;
//    }
//
//
//
//}
